package com.onlinemarket.thrid.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.onlinemarket.thrid.daoimpl.SupplierDaoImpl;
import com.onlinemarket.thrid.daoimpl.goodsDaoImpl;
import com.onlinemarket.thrid.daoimpl.warehouseDaoImpl;
import com.onlinemarket.thrid.domain.Goods;
import com.onlinemarket.thrid.domain.Supplier;
import com.onlinemarket.thrid.domain.WareHouse;

@Controller
@RequestMapping(value = "/goods")
public class goodsControl {
	@Autowired
	private goodsDaoImpl goodsdaoimpl;
	@Autowired
	private SupplierDaoImpl Supplierdaoimpl;
	@Autowired
	private warehouseDaoImpl warehousedaoimpl;

	@RequestMapping(value = "/addGoods", method = { RequestMethod.POST })
	public String addGoods(String goodsname, String goodprice, String goodsnumber, String supplierid,
			String warehouseid) {
		String page = "";
		Goods goods = new Goods(goodsname, Double.parseDouble(goodprice), Integer.parseInt(goodsnumber),
				Integer.parseInt(supplierid), Integer.parseInt(warehouseid));
		boolean insertsuccess = goodsdaoimpl.insertGood(goods);
		if (insertsuccess) {
			page = "success";
		} else {
			page = "fail";
		}
		return page;
	}

	/**
	 * 编辑商品信息
	 * 
	 * @param goods_id
	 * @return
	 */
	@RequestMapping(value = "/editgoods")
	public ModelAndView editgoods(Integer goods_id) {
		ModelAndView modelAndView = new ModelAndView();
		Goods goods = goodsdaoimpl.getGoodsById(goods_id);
		List<Supplier> allsupplier = Supplierdaoimpl.getallSupplierforlist();
		List<WareHouse> allwarehouse = warehousedaoimpl.getAllwarehouse();
		modelAndView.addObject("goods", goods);
		modelAndView.addObject("allsupplier", allsupplier);
		modelAndView.addObject("allwarehouse", allwarehouse);
		modelAndView.setViewName("goods/editgoods");
		return modelAndView;
	}

	/**
	 * 修改商品信息
	 * 
	 * @param goods_id
	 * @param goods_name
	 * @param goods_price
	 * @param goods_number
	 * @param supplier_id
	 * @param warehoue_id
	 * @return
	 * @author LHC
	 */
	@RequestMapping(value = "/updategoodsinfo")
	public ModelAndView UpdateGoodsInfo(int goods_id, String goods_name, double goods_price, int goods_number,
			int supplier_id, int warehoue_id) {
		String responseworld = "";
		ModelAndView modelAndView = new ModelAndView("goods/editgoods");
		Goods goods = new Goods(goods_id, goods_name, goods_price, goods_number, supplier_id, warehoue_id);
		boolean updatesuccess = goodsdaoimpl.updateGoodsinfo(goods);
		if (updatesuccess) {
			responseworld = "修改成功！";
		} else {
			responseworld = "请认证检查，并重新提交！";
		}
		modelAndView.addObject("responseworld", responseworld);
		return modelAndView;

	}

	/**
	 * 通过ID得到商品的详细信息
	 * 
	 * @param goods_id
	 * @return
	 */
	@RequestMapping(value = "/getallgoodsinfo")
	public ModelAndView getallgoodsinfo(int goods_id) {
		ModelAndView modelAndView = new ModelAndView();
		Goods goods = goodsdaoimpl.getGoodsById(goods_id);
		modelAndView.addObject("goods", goods);
		modelAndView.setViewName("/goods/viewgoodsInfo");
		return modelAndView;
	}

	/**
	 * 显示所有的商品
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getallgoods")
	public ModelAndView getallgoods() {
		ModelAndView modelAndView = new ModelAndView();
		List<Goods> allgoods = goodsdaoimpl.getallGoods();
		modelAndView.addObject("allgoods", allgoods);
		modelAndView.setViewName("goods/viewallgoods");
		return modelAndView;
	}

	/**
	 * 查看商品详细信息
	 * 
	 * @param goods_id
	 * @return
	 */
	@RequestMapping(value = "/viewgoods")
	public ModelAndView viewgoods(Integer goods_id) {
		ModelAndView modelAndView = new ModelAndView();
		goodsdaoimpl.getGoodsByIdforlist(goods_id);
		return modelAndView;
	}

	@RequestMapping(value = "/toaddpage")
	public ModelAndView toaddpage() {
		ModelAndView modelAndView = new ModelAndView();
		List<Supplier> allsupplier = Supplierdaoimpl.getallSupplierforlist();
		List<WareHouse> allwarehouse = warehousedaoimpl.getAllwarehouse();
		modelAndView.addObject("allsupplier", allsupplier);
		modelAndView.addObject("allwarehouse", allwarehouse);
		modelAndView.setViewName("goods/addgoods");
		return modelAndView;
	}

	@RequestMapping(value = "/doaddAction")
	public String doaddAction(String goodsname, double price, int number, int supplier, int warehouse) {
		String page = "";
		Goods goods = new Goods();
		goods.setGoods_name(goodsname);
		goods.setGoods_price(price);
		goods.setGoods_number(number);
		goods.setSupplier_id(supplier);
		goods.setWarehouse_id(warehouse);
		boolean isinsert = goodsdaoimpl.insertGood(goods);
		if (isinsert) {
			page = "success";
		} else {
			page = "fail";
		}
		return page;
	}
}
